package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.entity.GoodsType;
import com.qf.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package ${PACKAGE_NAME}
 * @description: ${TODO}
 * @date 2022/6/23 10:05
 */
@WebServlet(name = "GoodsTypeServlet",urlPatterns = "/goodstypeservlet")
public class GoodsTypeServlet extends BaseServlet {

    private GoodsTypeService goodsTypeService;

    @Autowired
    public void setGoodsTypeService(GoodsTypeService goodsTypeService) {
        this.goodsTypeService = goodsTypeService;
    }


    public String goodstypelist(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String json= (String) this.getServletContext().getAttribute("goodstype");
        if(json==null) {
            System.out.println("查询数据库...");
            //获取商品类别

            //2调用方法
            List<GoodsType> list = goodsTypeService.findByLevel(1);
            //3返回json
            json = JSON.toJSONString(list);//request、session、application
            //放入application
            this.getServletContext().setAttribute("goodstype", json);
        }
        //4返回
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
        return null;
    }
}
